import { defineStore } from 'pinia'

interface ScheduleItem {
  id: number
  // 标题
  title: string
  // 备注
  remark: string
  // 开始时间
  startAt: string
  // 结束时间
  endAt: string
  // 颜色
  color: string
}

interface WeekDate {
  month: number
  day: number
}

export const useSchedule = defineStore('schedule', {
  state: () => ({
    // 选中时间
    time: new Date(),
    // 选中类型
    type: 'day' as ScheduleMode,
    // 星期列表
    weekList: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    // 日程列表
    list: [
      {
        id: 1,
        color: '#f56c6c',
        title: '测试1',
        remark: '备注1',
        startAt: '2024-02-25 01:10:00',
        endAt: '2024-02-25 02:20:00'
      },
      {
        id: 2,
        color: '#8564df',
        title: '测试2',
        remark: '备注2',
        startAt: '2024-02-25 02:20:00',
        endAt: '2024-02-25 02:40:00'
      },
      {
        id: 3,
        color: '#8564df',
        title: '测试3',
        remark: '备注4',
        startAt: '2024-02-24 03:10:00',
        endAt: '2024-02-24 03:30:00'
      }
    ] as ScheduleItem[]
  }),
  actions: {
    // 格式化时间
    formatWeekDate(index: number, split: string) {
      const { month, day } = this.weekListDate[index]
      return (month < 10 ? '0' + month : month) + split + (day < 10 ? '0' + day : day)
    }
  },
  getters: {
    // 获取星期对应日期列表
    weekListDate() {
      const weekListDate = <WeekDate[]>[]
      // 获取时间的年份
      const year = this.time.getFullYear()
      // 获取时间的月份
      const month = this.time.getMonth() + 1
      // 获取时间的日期
      const day = this.time.getDate()
      // 最后一天
      const lastDay = Number(new Date(year, month, 0).getDate())

      // 获取时间星期几
      let whatDay = this.time.getDay()
      whatDay = whatDay === 0 ? 6 : whatDay - 1
      for (let i = 0; i < 7; i++) {
        let nowDay = day - whatDay + i
        let nowMonth = month
        // 当前日期大于本月最后一天
        if (nowDay > lastDay) {
          nowDay = nowDay - lastDay
          nowMonth = month + 1
          // 当前日期小于1号
        } else if (nowDay < 1) {
          nowDay = new Date(year, month - 1, 0).getDate() + nowDay
          nowMonth = month - 1
        }
        weekListDate.push({
          month: nowMonth,
          day: nowDay
        })
      }
      return weekListDate
    },
    // 获取选中时间为周几
    dayOfWeek(): string {
      let day = this.time.getDay()
      day = day === 0 ? 6 : day - 1
      console.log(day)
      console.log(this.weekList)

      console.log(this.weekList[day])

      return this.weekList[day]
    }
  }
})
